#!/bin/bash
manifest_url='http://dgggit09-rd.huawei.com/euleros/manifest.git'

git config --global credential.helper store
git config --global user.name peulerosci

function repo_config()
{
    if [ ! -d ".repo" ]; then
        rm -rf init_repo_euleros
        curl -O http://dgggit09-rd.huawei.com/static/init_repo_euleros
        chmod 755 init_repo_euleros
        expect -c "
        set timeout -1
        spawn ./init_repo_euleros peulerosci
        expect {
            \"*(yes/no)*\" { send \"yes\r\"; exp_continue }
            \"*Username*\" { send \"peulerosci\r\"; exp_continue }
            \"*Password*\" { send \"EulerOS_123\r\"; exp_continue }
            eof { catch wait result; exit [lindex \$result 3] }
        }
        expect {
            eof { catch wait result; exit [lindex \$result 3] }
        }
"
        rm -f init_repo_euleros
    fi
}

function repo_init()
{
    manifest=$1
    expect -c "
        set timeout -1
        spawn repo init -u $manifest_url -b master -m $manifest --no-repo-verify --repo-branch=euleros  --no-clone-bundle
        expect {
            \"*(yes/no)*\" { send \"yes\r\"; exp_continue }
            \"*Username*\" { send \"peulerosci\r\"; exp_continue }
            \"*Password*\" { send \"EulerOS_123\r\"; exp_continue }
            eof { catch wait result; exit [lindex \$result 3] }
        }
        expect {
            eof { catch wait result; exit [lindex \$result 3] }
        }
"
    if [ $? -ne 0 ]; then
        repo init -u $manifest_url -b master -m $manifest --no-repo-verify --repo-branch=euleros  --no-clone-bundle
    fi
}

function repo_create()
{
    repo_dir=$1
    manifest=$2
    pushd $repo_dir
    repo_config
    repo_init $manifest
    popd
}

repo_create "$1" "$2"
